home *** CD-ROM | disk | FTP | other *** search
/ PC World Komputer 2010 April / PCWorld0410.iso / pluginy Firefox / 7684 / 7684.xpi / resources / fmEntities.js < prev    next >
Text File  |  2009-11-20  |  8KB  |  248 lines

  1. /**
  2.  * Copyright (c) 2008, Jose Enrique Bolanos, Jorge Villalobos
  3.  * All rights reserved.
  4.  *
  5.  * Redistribution and use in source and binary forms, with or without
  6.  * modification, are permitted provided that the following conditions are met:
  7.  *
  8.  *  * Redistributions of source code must retain the above copyright notice,
  9.  *    this list of conditions and the following disclaimer.
  10.  *  * Redistributions in binary form must reproduce the above copyright notice,
  11.  *    this list of conditions and the following disclaimer in the documentation
  12.  *    and/or other materials provided with the distribution.
  13.  *  * Neither the name of Jose Enrique Bolanos, Jorge Villalobos nor the names
  14.  *    of its contributors may be used to endorse or promote products derived
  15.  *    from this software without specific prior written permission.
  16.  *
  17.  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  18.  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  19.  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  20.  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER
  21.  * OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
  22.  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
  23.  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
  24.  * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
  25.  * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
  26.  * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  27.  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  28.  **/
  29.  
  30. var EXPORTED_SYMBOLS = [];
  31.  
  32. const Cc = Components.classes;
  33. const Ci = Components.interfaces;
  34. const Ce = Components.Exception;
  35.  
  36. Components.utils.import("resource://gre/modules/XPCOMUtils.jsm");
  37. Components.utils.import("resource://firefm/fmCommon.js");
  38.  
  39. // URL templates used to generate special Last.fm station URLs.
  40. const STATION_URL_ARTIST = "lastfm://artist/$(ARTIST)/similarartists";
  41. const STATION_URL_RECOMMENDED = "lastfm://user/$(USER)/recommended";
  42. const STATION_URL_USER = "lastfm://user/$(USER)/personal";
  43. const STATION_URL_TAG = "lastfm://globaltags/$(TAG)";
  44. const STATION_URL_NEIGHBORHOOD = "lastfm://user/$(USER)/neighbours";
  45. const STATION_URL_LOVED = "lastfm://user/$(USER)/loved";
  46.  
  47. /**
  48.  * Station info object. Represents a Last.FM station.
  49.  */
  50. FireFM.StationInfo = function(aId, aType) {
  51.   this._logger = FireFM.getLogger("FireFM.StationInfo");
  52.   this._logger.debug("init");
  53.  
  54.   this._id = FireFM.decodeFMString(aId);
  55.   this._type = aType;
  56.  
  57.   switch (aType) {
  58.     case FireFM.Station.TYPE_ARTIST:
  59.       this._title =
  60.         FireFM.overlayBundle.formatStringFromName(
  61.           "firefm.station.artist.label", [ this._id ], 1);
  62.       break;
  63.     case FireFM.Station.TYPE_RECOMMENDED:
  64.       this._title =
  65.         FireFM.overlayBundle.formatStringFromName(
  66.           "firefm.station.recommended.label", [ this._id ], 1);
  67.       break;
  68.     case FireFM.Station.TYPE_USER:
  69.       this._title =
  70.         FireFM.overlayBundle.formatStringFromName(
  71.           "firefm.station.user.label", [ this._id ], 1);
  72.       break;
  73.     case FireFM.Station.TYPE_TAG:
  74.       this._title =
  75.         FireFM.overlayBundle.formatStringFromName(
  76.           "firefm.station.tag.label", [ this._id ], 1);
  77.       break;
  78.     case FireFM.Station.TYPE_NEIGHBORHOOD:
  79.       this._title =
  80.         FireFM.overlayBundle.formatStringFromName(
  81.           "firefm.station.neighborhood.label", [ this._id ], 1);
  82.       break;
  83.     case FireFM.Station.TYPE_LOVED:
  84.       this._title =
  85.         FireFM.overlayBundle.formatStringFromName(
  86.           "firefm.station.loved.label", [ this._id ], 1);
  87.       break;
  88.   }
  89. };
  90.  
  91. /**
  92.  * StationInfo object methods.
  93.  */
  94. FireFM.StationInfo.prototype = {
  95.   /* Private copy of the id. */
  96.   _id : null,
  97.   /* Private copy of the station type. */
  98.   _type : null,
  99.   /* Private copy of the title. */
  100.   _title : null,
  101.  
  102.   /* Gets the station id. */
  103.   get id() { return this._id; },
  104.  
  105.   /* Gets the station type. */
  106.   get type() { return this._type; },
  107.  
  108.   /* Gets the station title. */
  109.   get title() { return this._title; },
  110.  
  111.   /**
  112.    * Gets the wrapped inner object.
  113.    * XXX: this is a workaround so I can pass this object through an observer
  114.    * without having to explicitly declare an interface for it.
  115.    * http://www.mail-archive.com/dev-tech-xpcom@lists.mozilla.org/msg01505.html
  116.    */
  117.   get wrappedJSObject() { return this; },
  118.  
  119.   /**
  120.    * Generates a special type of URL that Last.fm handles for stations.
  121.    * @return the special URL that corresponds to this station.
  122.    */
  123.   getStationURL : function() {
  124.     this._logger.debug("getStationURL");
  125.  
  126.     let url = null;
  127.  
  128.     switch (this.type) {
  129.       case FireFM.Station.TYPE_ARTIST:
  130.         url =
  131.           STATION_URL_ARTIST.replace(
  132.             /\$\(ARTIST\)/, encodeURIComponent(this.id));
  133.         break;
  134.       case FireFM.Station.TYPE_RECOMMENDED:
  135.         url =
  136.           STATION_URL_RECOMMENDED.replace(
  137.             /\$\(USER\)/, encodeURIComponent(this.id));
  138.         break;
  139.       case FireFM.Station.TYPE_USER:
  140.         url =
  141.           STATION_URL_USER.replace(/\$\(USER\)/, encodeURIComponent(this.id));
  142.         break;
  143.       case FireFM.Station.TYPE_TAG:
  144.         url = STATION_URL_TAG.replace(/\$\(TAG\)/, encodeURIComponent(this.id));
  145.         break;
  146.       case FireFM.Station.TYPE_NEIGHBORHOOD:
  147.         url =
  148.           STATION_URL_NEIGHBORHOOD.replace(
  149.             /\$\(USER\)/, encodeURIComponent(this.id));
  150.         break;
  151.       case FireFM.Station.TYPE_LOVED:
  152.         url =
  153.           STATION_URL_LOVED.replace(/\$\(USER\)/, encodeURIComponent(this.id));
  154.         break;
  155.     }
  156.  
  157.     return url;
  158.   },
  159.  
  160.   /**
  161.    * Returns the JSON representation of this object.
  162.    * @returns JSON representation of this object.
  163.    */
  164.   toJSON : function() {
  165.     this._logger.debug("toJSON");
  166.  
  167.     let that = this;
  168.     let jsonObj =
  169.       { id : that._id, type : that._type, title : that._title };
  170.  
  171.     return JSON.stringify(jsonObj);
  172.   },
  173.  
  174.   /**
  175.    * We need to pass tracks through observers, so we implement nsISupports.
  176.    */
  177.   QueryInterface: XPCOMUtils.generateQI([Ci.nsISupports])
  178. };
  179.  
  180. /**
  181.  * Track object. Represents a Last.FM track.
  182.  */
  183. FireFM.Track = function(
  184.   aId, aLocation, aTitle, aRecording, aAlbumTitle, aArtist, aDuration,
  185.   aImagePath, aTrackAuth, aAlbumId, aArtistId, aArtistURL, aAlbumURL, aTrackURL,
  186.   aBuyTrackURL, aBuyAlbumURL, aFreeTrackURL) {
  187.   this.id = aId;
  188.   this.location = aLocation;
  189.   this.title = aTitle;
  190.   this.recording = aRecording;
  191.   this.albumTitle = aAlbumTitle;
  192.   this.artist = aArtist;
  193.   this.duration = aDuration;
  194.   this.imagePath = aImagePath;
  195.   this.trackAuth = aTrackAuth;
  196.   this.albumId = aAlbumId;
  197.   this.artistId = aArtistId;
  198.   this.artistURL = aArtistURL;
  199.   this.albumURL = aAlbumURL;
  200.   this.trackURL = aTrackURL;
  201.   this.buyTrackURL = aBuyTrackURL;
  202.   this.buyAlbumURL = aBuyAlbumURL;
  203.   this.freeTrackURL = aFreeTrackURL;
  204.   // The time the track began playing.
  205.   this.startTime = -1;
  206. };
  207.  
  208. /**
  209.  * Track object methods.
  210.  */
  211. FireFM.Track.prototype = {
  212.   /* Video for this track (optionally loaded from Playlist) */
  213.   video : null,
  214.  
  215.   /**
  216.    * Gets the wrapped inner object.
  217.    * XXX: this is a workaround so I can pass this object through an observer
  218.    * without having to explicitly declare an interface for it.
  219.    * http://www.mail-archive.com/dev-tech-xpcom@lists.mozilla.org/msg01505.html
  220.    */
  221.   get wrappedJSObject() {
  222.     return this;
  223.   },
  224.  
  225.   /**
  226.    * We need to pass tracks through observers, so we implement nsISupports.
  227.    */
  228.   QueryInterface: XPCOMUtils.generateQI([Ci.nsISupports])
  229. };
  230.  
  231. /**
  232.  * User object. Represents a Last.FM user.
  233.  */
  234. FireFM.User = function(aName, aURL, aImagePath) {
  235.   this.name = aName;
  236.   this.url = aURL;
  237.   this.imagePath = aImagePath;
  238. };
  239.  
  240. /**
  241.  * Artist object. Represents a Last.FM artist.
  242.  */
  243. FireFM.Artist = function(aName, aURL, aImagePath) {
  244.   this.name = aName;
  245.   this.url = aURL;
  246.   this.imagePath = aImagePath;
  247. };
  248.